package com.gp.testonline.controller;

import com.gp.testonline.eo.RoleEOPage;
import com.gp.testonline.model.Role;
import com.gp.testonline.service.RoleService;
import com.gp.testonline.tool.ResponseMessage;
import com.gp.testonline.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对外提供数据服务接口
 */
@Api(tags = "role")
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "角色添加方法")
    @RequestMapping(value = "/addRole",method = RequestMethod.GET)
    public ResponseMessage add(Role role){
        int IsAdd=0;
        //传入数据的判断
        //（1）实体对象不能为空
        if (role!=null){
            //(2) 角色名称不能为空
            if (role.getRname()!=null){
                //(3)添加角色数据
                IsAdd=roleService.AddRole(role);
                if (IsAdd==1){
                    return Result.success("保存成功！");
                }else if (IsAdd==-1){
                    return Result.error("角色重复不允许添加！");
                }else{
                    return Result.error("保存失败！");
                }
            }
        }
        return Result.error("保存失败！");
    }

    @ApiOperation(value = "角色删除方法")
    @RequestMapping(value = "/deleteRole",method = RequestMethod.GET)
    public ResponseMessage deleteRole(int rid){
        if (rid!=0){
            int deleteInt=0;
            deleteInt=roleService.DeleteRole(rid);
            if (deleteInt>0){
                return Result.success("删除成功！");
            }else {
                return Result.error("删除的角色与人员角色权限表有关联,删除失败！");
            }
        }else{
            return Result.error("删除的主键不能为0，请重新输入！");
        }
    }

    @ApiOperation(value = "角色修改方法")
    @RequestMapping(value = "/updateRole",method = RequestMethod.GET)
    public ResponseMessage updateRole(Role role){
        int isUpdate=0;
        //传入的参数不能为空
        if(role!=null){
            //参数当中主键不能为0
            if (role.getRid()!=0){
                isUpdate=roleService.UpdateRole(role);
                if (isUpdate==1){
                    return Result.success("更新成功！");
                }else {
                    return Result.error("更新失败！");
                }
            }else {
                return Result.error("传入的参数不正确");
            }
        }
        return Result.error("传入的参数不正确");
    }

    @ApiOperation(value = "根据条件获取角色信息")
    @RequestMapping(value = "/getAll",produces = "application/json;charest=utf-8",method = RequestMethod.PUT)
    public ResponseMessage getAll( RoleEOPage roleEOPage){
        Map<String, List> map=new HashMap<>();
        List<RoleEOPage> listRole=roleService.getWhereAll(roleEOPage);
        map.put("list",listRole);
        return Result.success("200",map);
    }



}
